﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Serialization.Json;
using System.IO;

namespace CaptialVices
{
    [DataContract]
    class Level : ICustomUpdatable, ICustomDrawable
    {
        [DataMember]
        public List<Layer> layers;
        public Camera camera;
        public Player player;
        public World world;
        public SkillBar skillBar;
        public CurrentPowerDisplay currentPowerDisplay;

        private bool playerDroppedOnce = false;

        public void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            KeyboardState keyboardState = Keyboard.GetState();

            if (player.OnGround())
            {
                playerDroppedOnce = true;
            }

            float y = 0;
            if (playerDroppedOnce)
            {
                y = player.LinearSpeed.Y * elapsedTime * 50;
            }

            Vector2 move = new Vector2(player.LinearSpeed.X * elapsedTime * 25, y);

            camera.Move(move, true);
            world.ShiftOrigin(CoordinateHelper.ToWorld(move));

            if (keyboardState.IsKeyDown(Keys.LeftControl) && !Input.lastKeyboardState.IsKeyDown(Keys.LeftControl))
            {
                player.Jump();
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                player.Move(Movement.Left);
            }
            else if (keyboardState.IsKeyDown(Keys.Right))
            {
                player.Move(Movement.Right);
            }
            else
            {
                player.Move(Movement.Stop);
            }

            Input.lastKeyboardState = keyboardState;

            world.Step((float)gameTime.ElapsedGameTime.TotalSeconds);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            foreach (Layer layer in layers)
                layer.Draw(spriteBatch, gameTime);

            skillBar.Draw(spriteBatch, gameTime);

            currentPowerDisplay.Draw(spriteBatch);
        }

        public void ChangeBlockTypeAtIndex(int index, String blockType, Texture2D texture)
        {
            Layer lastLayer = layers.Last();
            DrawablePhysicsObject block = lastLayer.Sprites[index] as DrawablePhysicsObject;
            //block.texture = texture;
        }

        public void ChangeBlockHeightAtIndex(int index, int height)
        {
            Layer lastLayer = layers.Last();
            DrawablePhysicsObject block = lastLayer.Sprites[index] as DrawablePhysicsObject;
            block.Position  = new Vector2(block.Position.X, height);
        }

        public static Level ParseLevelFile(String levelFile)
        {
            return null;
        }

        public static void WriteLevelToFile(Level level, String fileName)
        {
            //DataContractJsonSerializer serializer;

            //MemoryStream stream = new MemoryStream();
            //serializer = new DataContractJsonSerializer(typeof(Level));

            //serializer.WriteObject(stream, level);

            //stream.Position = 0;

            //StreamReader streamReader = new StreamReader(stream);
            //Console.WriteLine(streamReader.ReadToEnd());

            //Console.WriteLine();
        }
    }
}
